<template>
  <!-- 新增部门的弹层 -->
  <el-dialog :title="formData.id?'编辑部门':'新增部门'" :visible="dialogVisible" @close="del">
    <!-- 表单组件  el-form   label-width设置label的宽度   -->
    <!-- 匿名插槽 -->
    <el-form ref="addDept" label-width="120px" :model="formData" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="formData.name" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="formData.code" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="formData.manager" style="width:80%" placeholder="请选择" @blur="adtianj">
          <el-option v-for="val in employeeList" :key="val.id" :value="val.username" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="formData.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>
    </el-form>
    <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
    <el-row slot="footer" type="flex" justify="center">
      <!-- 列被分为24 -->
      <el-col :span="6">
        <el-button type="primary" size="small" @click.native="adden">确定</el-button>
        <el-button size="small" @click.native="del">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { department, datedepartment } from '@/api/departments'
import { simple, departmentst } from '@/api/employees'
export default {
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    nodeData: {
      type: Object,
      required: true
    }
  },
  data() {
    const checkNameRepeat = async(rule, val, callback) => {
      const { depts } = await department()
      const depart = depts.filter(item => item.pid === this.nodeData.id).some(item => item.name === val && item.id !== this.nodeData.id)
      depart ? callback(new Error('不能有同个部门')) : callback()
    }
    const differentencoding = async(rule, val, callback) => {
      const { depts } = await department()
      const depart = depts.some(item => item.code === val && item.id !== this.nodeData.id)
      depart ? callback(new Error('公司已经有此编码')) : callback()
    }
    return {
      formData: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '', // 部门介绍
        pid: ''
      },
      employeeList: [],
      rules: {
        name: [{ required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: 'blur' },
          { validator: checkNameRepeat, trigger: 'blur' }],
        code: [{ required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: 'blur' },
          { validator: differentencoding, trigger: 'blur' }],
        manager: [{ required: true, message: '部门负责人不能为空', trigger: 'blur' }],
        introduce: [{ required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { trigger: 'blur', min: 1, max: 300, message: '部门介绍要求1-50个字符' }]
      }
    }
  },
  created() {
    simple().then(res => {
      this.employeeList = res
    })
  },
  methods: {
    adtianj() {
      setTimeout(() => {
        this.$refs.addDept.validateField('manager')
      }, 200)
    },
    adden() {
      this.$refs.addDept.validate(async isOK => {
        if (isOK) {
          // 表示可以提交了
          if (this.formData.id) {
            await datedepartment(this.formData)
            this.$emit('add')
            this.$emit('update:dialogVisible', false)
          } else {
            await departmentst({ ...this.formData, pid: this.nodeData.id }) // 调用新增接口 添加父部门的id
            this.$emit('add')
            this.$emit('update:dialogVisible', false)
          }
        }
      })
    },
    del() {
      this.$refs.addDept.resetFields()
      this.$emit('update:dialogVisible', false)
      this.formData = {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '', // 部门介绍
        pid: ''
      }
    },
    getDepartmentDetail(nodeData) {
      this.formData = { ...nodeData } // ??????? 问老师
    }
  }
}
</script>

<style>

</style>
